package com.zcurd.common.plugin;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Config;
import com.jfinal.plugin.activerecord.DbKit;
import org.quartz.utils.ConnectionProvider;


import java.sql.Connection;
import java.sql.SQLException;

/**
 * .@author xpg
 */
public class QuartzConnectionProvider implements ConnectionProvider {

  private Log logger = Log.getLog(QuartzPlugin.class); 
  private Config config;


  public Connection getConnection() throws SQLException {
    return config.getConnection();
  }


  public void initialize() throws SQLException {
    config = DbKit.getConfig(QuartzPlugin.dsName);
    if (config == null) {
      throw new RuntimeException("quartz datasource  not found");
    }
  }


  public void shutdown() throws SQLException {
    if (QuartzPlugin.dsAlone)
      config.getConnection().close();
    else
      logger.info("quartz datasource is not alone,so you cant close it");
  }

}